package cn.hftoday.org;

import java.util.*;
import org.apache.log4j.*;
import cn.hftoday.db.*;
import cn.hftoday.util.*;
import cn.hftoday.core.*;

/**
 * login account
 * @author lane.cn@gmail.com
 *
 */
public class Account {
	private static final Logger LOG = Logger.getLogger(Account.class);
	
	private String id;
	private String name;
	private String email;
	private String password;
	private Calendar createTime = CalendarParser.INVALID_TIME;
	private String personId;
	private String companyId;
	private boolean valid;
	
	/**
	 * account id
	 * @return
	 */
	public String getId() {
		return id;
	}
	
	/**
	 * account id
	 * @param id
	 */
	public void setId(String id) {
		this.id = id;
	}
	
	/**
	 * name
	 * @return
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * name
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * email
	 * @return
	 */
	public String getEmail() {
		return email;
	}
	
	/**
	 * email
	 * @param email
	 */
	public void setEmail(String email) {
		this.email = email;
	}
	
	/**
	 * password
	 * @return
	 */
	public String getPassword() {
		return password;
	}
	
	/**
	 * password
	 * @param password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * create time
	 * @return
	 */
	public Calendar getCreateTime() {
		return createTime;
	}
	
	/**
	 * create time
	 * @param createTime
	 */
	public void setCreateTime(Calendar createTime) {
		this.createTime = createTime;
	}
	
	/**
	 * person
	 * @return
	 */
	public Person getPerson() {
		return (Person)Mapper.getInstance().queryForObject("SelectPersonById", personId);
	}
	
	/**
	 * person
	 * @param person
	 */
	public void setPerson(Person person) {
		if (person == null) {
			personId = null;
		}
		else {
			personId = person.getId();
		}
	}
	
	/**
	 * company
	 * @return
	 */
	public Company getCompany() {
		return (Company)Mapper.getInstance().queryForObject("SelectCompanyById", companyId);
	}
	
	/**
	 * company
	 * @param company
	 */
	public void setCompany(Company company) {
		if (company == null) {
			companyId = null;
		}
		else {
			companyId = company.getId();
		}
	}
	
	public boolean isValid() {
		return valid;
	}
	
	public void setValid(boolean valid) {
		this.valid = valid;
	}
	
	private static void _select(Object object, RecordSet rs, int i) {
		Account o = (Account)object;
		o.id = rs.getItem(i, "f_id");
		o.name = rs.getItem(i, "f_name");
		o.email = rs.getItem(i, "f_email");
		o.password = rs.getItem(i, "f_password");
		o.createTime = CalendarParser.toCalendar(rs.getItem(i, "f_createtime"));
		o.personId = rs.getItem(i, "f_person");
		o.companyId = rs.getItem(i, "f_company");
		o.valid = rs.getItem(i, "f_valid").equals("1");
	}
	
	private static String _insert(Object object, String sql) {
		Account o = (Account)object;
		return StringParser.formatSql(sql, 
			o.id,
			o.name,
			o.email,
			o.password,
			CalendarParser.toString(o.createTime),
			o.personId,
			o.companyId,
			o.valid ? "1" : "0");
	}
	
	private static String _update(Object object, String sql) {
		Account o = (Account)object;
		return StringParser.formatSql(sql, 
			o.id,
			o.name,
			o.email,
			o.password,
			CalendarParser.toString(o.createTime),
			o.personId,
			o.companyId,
			o.valid ? "1" : "0");
	}
	
	private static String _delete(Object object, String sql) {
		Account o = (Account)object;
		return StringParser.formatSql(sql, 
			o.id);
	}
}